import { expr } from '@/utils/url'
import Vue from 'vue'
import HttpPlugin from './src'
import { getInitOptions } from '@/config/init'
import { AxiosRequestConfig } from 'axios'

const appId = process.env.VUE_APP_APP_ID

Vue.use(HttpPlugin, {
  baseURL: getInitOptions()?.api,
  requestHandler: function (cfg: AxiosRequestConfig) {
    const token = new URL(location.href).searchParams.get('token') || localStorage.getItem('token')
    if (token) {
      const headers = { 'rawa-sid': token }
      cfg.headers = Object.assign(cfg.headers || {}, headers)
      localStorage.setItem('token', token)
    }
  },
  expiredHandler: function (url: string) {
    const auth = Vue.prototype.$auth
    if (auth) auth.toLogin(expr(url), false)
  }
})
